package actionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import util.DomainUtils;

public class UpdateContactValidationForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	private String selected_contact = null ;
	private String selected_contact_version = null;
	private String first_name = null;
	private String last_name = null;
	private String e_mail = null;
	private String street = null;
	private String city = null;
	private String zip = null;
	private String country = null;
	private String mobile = null;
	private String house = null;
	private String work = null;
	private String num_siret = null;
	private String[] contactgroups = null;
	
	
	
	public String getSelected_contact() {
		return selected_contact;
	}

	public void setSelected_contact(String selected_contact) {
		this.selected_contact = selected_contact;
	}
	
	public String getSelected_contact_version() {
		return selected_contact_version;
	}

	public void setSelected_contact_version(String selected_contact_version) {
		this.selected_contact_version = selected_contact_version;
	}
	
	public String getFirst_name() {
		return first_name;
	}
	public void setFirst_name(String first_name) {
		this.first_name = DomainUtils.trim(first_name);
	}

	public String getLast_name() {
		return last_name;
	}

	public void setLast_name(String last_name) {
		this.last_name = DomainUtils.trim(last_name);
	}

	public String getE_mail() {
		return e_mail;
	}

	public void setE_mail(String e_mail) {
		this.e_mail = DomainUtils.trim(e_mail);
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = DomainUtils.trim(street);
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = DomainUtils.trim(city);
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = DomainUtils.trim(zip);
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = DomainUtils.trim(country);
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = DomainUtils.trim(mobile);
	}

	public String getHouse() {
		return house;
	}

	public void setHouse(String house) {
		this.house = DomainUtils.trim(house);
	}

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = DomainUtils.trim(work);
	}

	public String getNum_siret() {
		return num_siret;
	}

	public void setNum_siret(String num_siret) {
		this.num_siret = DomainUtils.trim(num_siret);
	}

	public String[] getContactgroups() {
		return contactgroups;
	}

	public void setContactgroups(String[] contactgroups) {
		this.contactgroups = contactgroups;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.first_name = null;
		this.last_name = null;
		this.e_mail = null;
		this.street = null;
		this.city = null;
		this.zip = null;
		this.country = null;
		this.mobile = null;
		this.house = null;
		this.work = null;
		this.num_siret = null;
		this.contactgroups = null;
	}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession(false);

		// Checking session
		if (session == null
				|| session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"session.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking firstname
		if (first_name == null
				|| first_name.isEmpty()
				|| DomainUtils.match(first_name, "[a-zA-Z]+(\\-[a-zA-Z]*)?") == false) {
			
			errors.add(
					"first_name",
					new ActionMessage(
							"contact.first_name.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking lastname
		if (last_name == null
				|| last_name.isEmpty()
				|| DomainUtils.match(last_name,
						"[a-zA-Z]+(( [a-zA-Z]+)|(\\-[a-zA-Z]+))*") == false) {
			errors.add(
					"last_name",
					new ActionMessage(
							"contact.last_name.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking email

		// already checked in jsp
		if (e_mail == null || e_mail.isEmpty()) {
			errors.add(
					"e_mail",
					new ActionMessage(
							"contact.e_mail.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking street
		if (street == null || street.isEmpty()) {
			errors.add(
					"street",
					new ActionMessage(
							"contact.street.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking city
		if (city == null
				|| city.isEmpty()
				|| DomainUtils.match(city,
						"[a-zA-Z]+(( [a-zA-Z]+)|(\\-[a-zA-Z]+))*") == false) {
			errors.add(
					"city",
					new ActionMessage(
							"contact.city.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking zip
		if (zip == null || zip.isEmpty() || zip.length() != 5
				|| DomainUtils.match(zip, "\\d*") == false) {
			
			errors.add(
					"zip",
					new ActionMessage(
							"contact.zip.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking country
		if (country == null
				|| country.isEmpty()
				|| DomainUtils.match(country,
						"[a-zA-Z]+(( [a-zA-Z]+)|(\\-[a-zA-Z]+))*") == false) {		
			errors.add(
					"country",
					new ActionMessage(
							"contact.country.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		// Checking mobile
		if (!mobile.isEmpty()) {
			if (mobile == null || mobile.isEmpty() || mobile.length() != 10
					|| DomainUtils.match(mobile, "\\d*") == false) {
				errors.add(
						"mobile",
						new ActionMessage(
								"contact.mobile.error"));
				
				DomainUtils.keepParametersContent(request);
				
				return errors;
			}
		}

		// Checking house
		if (!house.isEmpty()) {
			if (house == null || house.isEmpty() || house.length() != 10
					|| DomainUtils.match(house, "\\d*") == false) {
				errors.add(
						"house",
						new ActionMessage(
								"contact.house.error"));
				
				DomainUtils.keepParametersContent(request);
				
				return errors;
			}
		}

		// Checking work
		if (!work.isEmpty()) {
			if (work == null || work.isEmpty() || work.length() != 10
					|| DomainUtils.match(work, "\\d*") == false) {
				errors.add(
						"work",
						new ActionMessage(
								"contact.work.error"));
				
				DomainUtils.keepParametersContent(request);
				
				return errors;
			}
		}

		// no need to check the contactgroups

		// Checking siret
		if (num_siret != null) {
			if (DomainUtils.match(num_siret, "\\d*") == false) {
				errors.add(
						"work",
						new ActionMessage(
								"contact.num_siret.error"));
				
				DomainUtils.keepParametersContent(request);
				
				return errors;
			}
		}
		
		
		return errors;

	}

}
